package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink13_Watermark_Sideout_2 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        SingleOutputStreamOperator<String> mainStream = env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            })
            .process(new ProcessFunction<WaterSensor, String>() {
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    // sensor1 放主流 sensor2 放侧输出流
                    if ("sensor_1".equals(value.getId())) {
                        out.collect(value.toString());
                    }else if("sensor_2".equals(value.getId())){
                        ctx.output(new OutputTag<String>("s2") {}, value.toString());
                    }
                }
            });
    
        mainStream.print("s1");
        mainStream.getSideOutput(new OutputTag<String>("s2") {}).print("s2");
    
        env.execute();
    }
}
/*
flink如何做到数据不丢失:
1. 事件时间+水印
2. 允许迟到数据
3. 侧输出流



 */